//
//  LoginWithEmailView.swift
//  XGamesLive
//
//  Created by ios on 2024/5/14.
//

import UIKit
import MXCommonModule

protocol LoginWithEmailDelegate: NSObjectProtocol {
    func createAccount()
    func login()
    func forgetPassword()
}

class LoginWithEmailView: UIView {
    weak var delegate: LoginWithEmailDelegate?
    private var emailText: UITextField!
    private var passwordText: UITextField!
    
    var email: String{
        guard let text = self.emailText.text else { return "" }
        return text
    }
    
    var password: String{
        guard let text = self.passwordText.text else { return "" }
        return text
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.createXglView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension LoginWithEmailView{
    @objc private func forgetAction(){
        self.delegate?.forgetPassword()
    }
    @objc private func createAccountAction(){
        self.delegate?.createAccount()
    }
    @objc private func loginAction(){
        self.delegate?.login()
    }
}

extension LoginWithEmailView{
    private func createXglView(){
        let iconView = LoginUntil.creatLoginIconView()
        self.emailText = UITextField().then({ make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
            make.attributedPlaceholder = "Enter here".attributesColor(UIColor.hexColor("#A2A2A2"),UIFont.pingfangSC_Regular(fontSize: 16.0))
        })
        let emailView = LoginUntil.creatLoginInputView(title: "Email", inputText: self.emailText)

        self.passwordText = UITextField().then({ make in
            make.textColor = UIColor.hexColor("#FF562E")
            make.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
            make.attributedPlaceholder = "Enter here".attributesColor(UIColor.hexColor("#A2A2A2"),UIFont.pingfangSC_Regular(fontSize: 16.0))
        })
        let passwordView = LoginUntil.creatLoginInputView(title: "Password", inputText: passwordText)
        let forgetButton = UIButton().then { make in
            make.setTitle("Forget Password", for: .normal)
            make.setTitleColor(UIColor.hexColor("#A2A2A2"), for: .normal)
            make.titleLabel?.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
            make.addTarget(self, action: #selector(self.forgetAction), for: .touchUpInside)
        }
        let createView = UIView()
        let createHeadText = UILabel().then { make in
            make.text = "Don’t have account?"
            make.font = UIFont.pingfangSC_Regular(fontSize: 14.0)
            make.textColor = .white
        }
        let createButton = UIButton().then { make in
            make.setTitle("Create one", for: .normal)
            make.setTitleColor(UIColor.hexColor("#80FFE6"), for: .normal)
            make.titleLabel?.font = UIFont.pingfangSC_Regular(fontSize: 14.0)
            make.addTarget(self, action: #selector(self.createAccountAction), for: .touchUpInside)
        }
        let loginButton = UIButton().then { make in
            make.setTitle("Sign in", for: .normal)
            make.setTitleColor(UIColor.hexColor("#191A1D"), for: .normal)
            make.titleLabel?.font = UIFont.pingfangSC_Regular(fontSize: 18.0)
            make.backgroundColor = UIColor.hexColor("#80FFE6")
            make.layerCornerRadius = 24.0
            make.addTarget(self, action: #selector(self.loginAction), for: .touchUpInside)
        }
        createView.addSubviews([createHeadText,createButton])
        self.addSubviews([iconView,emailView,passwordView,forgetButton,createView,loginButton])
        
        iconView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(conversionWithScrennWidth(value: 140.0))
            make.width.height.equalTo(conversionWithScrennWidth(value: 100.0))
        }
        emailView.snp.makeConstraints { make in
            make.left.equalTo(15.0)
            make.top.equalTo(iconView.snp.bottom).offset(conversionWithScrennWidth(value: 60.0))
            make.centerX.equalToSuperview()
        }
        passwordView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.left.equalTo(15.0)
            make.top.equalTo(emailView.snp.bottom).offset(conversionWithScrennWidth(value: 32.0))
        }
        forgetButton.snp.makeConstraints { make in
            make.right.equalTo(passwordView)
            make.top.equalTo(passwordView.snp.bottom).offset(10.0)
            make.height.equalTo(30.0)
        }
        createHeadText.snp.makeConstraints { make in
            make.left.centerY.equalToSuperview()
        }
        createButton.snp.makeConstraints { make in
            make.left.equalTo(createHeadText.snp.right)
            make.centerY.height.equalToSuperview()
        }
        createView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.height.equalTo(20.0)
            make.right.equalTo(createButton)
            make.bottom.equalTo(loginButton.snp.top).offset(-30.0)
        }
        loginButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(conversionWithScrennWidth(value: 345.0))
            make.height.equalTo(48.0)
            make.bottom.equalTo(-BottomHeight-48.0)
        }
    }
}
